package com.wang.myapplication.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wang.myapplication.R;
import com.wang.myapplication.base.BaseActivity;
import com.wang.myapplication.base.MyApp;
import com.wang.myapplication.network.HttpRequest;
import com.wang.myapplication.network.bean.LoginResponse;
import com.wang.myapplication.network.bean.Userinfo;
import com.wang.myapplication.tools.SharedPreTool;
import com.wang.myapplication.tools.StringTool;

public class UserLoginActivity extends BaseActivity {

    private EditText etUserName, etUserPwd;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public void initParams(Bundle parms) {
        if (SharedPreTool.getValue(SharedPreTool.KEY_USERINFO, SharedPreTool.STRING) != null) {
            String userInfo = (String) SharedPreTool.getValue(SharedPreTool.KEY_USERINFO, SharedPreTool.STRING);
            Userinfo userInfoObj = new Gson().fromJson(userInfo, Userinfo.class);
            $Log("SharedPreTool取出:"+userInfoObj);
            MyApp.getMyApp().setUserinfo(userInfoObj);
            HomeActivity.startActSendParams(this, "");
            this.finish();
            return;
        }

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_user_login;
    }

    @Override
    public void initView(Bundle savedInstanceState, View view) {
        etUserName = findViewById(R.id.etUserName);
        etUserPwd = findViewById(R.id.etUserPwd);
        btnLogin = findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(this);
    }


    @Override
    public void doBusiness(Context mContext) {
    }

    @Override
    public void widgetClick(View v) {
        if (!StringTool.isInputValid(etUserName.getText())) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return;
        } else if (!StringTool.isInputValid(etUserPwd.getText())) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        String input_uid = String.valueOf(etUserName.getText());
        String input_pwd = String.valueOf(etUserPwd.getText());
        reqLoginHttp(input_uid, input_pwd);



    }


    private void reqLoginHttp(String input_uid, String input_pwd) {
        HttpRequest.reqLoginRequest(new MyHttpback<LoginResponse>() {
            @Override
            public void onSuccessBack(LoginResponse obj) {
                SharedPreTool.putValue(SharedPreTool.KEY_USERINFO,obj);
                HomeActivity.startActSendParams(mCurActivity, "");
                mCurActivity.finish();
            }
        }, input_uid, input_pwd);

    }

}
